'''
Created on 2020年1月30日
获取所有股票数据
@author: JM
'''
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine 

engine_ts = create_engine('mysql://root:pig%401994@127.0.0.1:8306/tushare?charset=utf8&use_unicode=1')

def read_data():
    sql = """SELECT * FROM trade_cal"""
    df = pd.read_sql_query(sql, engine_ts)
    return df


def write_data(df):
    res = df.to_sql('trade_cal', engine_ts, index=False, if_exists='append', chunksize=5000)
    print(res)


def get_data():
    pro = ts.pro_api()
    data = pro.trade_cal(exchange='SSE', is_open='1', start_date='20221101', end_date='20231031', fields='cal_date')
    return data


if __name__ == '__main__':
#     df = read_data()
    df = get_data()
    write_data(df)
    print(df)